import java.io.*;
import java.util.Scanner;

public class demo12 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入要复制的源文件路径");
        String srcPath = scanner.next();
        System.out.println("输入要复制到的目标路径");
        String destPath = scanner.next();
        //先判断下源文件路劲存在文件与否
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);
        if(!srcFile.isFile()){
            System.out.println("源文件不存在/不是文件是目录,复制失败退出");
            return;
        }
        //判断目标路径的父目录存在与否
        if(!destFile.getParentFile().isDirectory()){
            System.out.println("目标路径不存在,复制失败退出");
            return;
        }
        //现在真正开复制操作

       try(InputStream inputStream = new FileInputStream(srcFile); OutputStream outputStream = new FileOutputStream(destFile)){
           while(true){
              byte[] buffer = new byte[1024];//缓存区
               int n =inputStream.read(buffer);
               if(n==-1){
                   //已经读完了
                   break;
               }
               //读一份写一份
               outputStream.write(buffer);
           }
           if(!destFile.exists()){
               //创建个文件
               srcFile.createNewFile();
           }
           System.out.println("文件复制完成");
       }
    }


}
